package com.wyh.lotterysystem.service.activitystatus.operator;

import com.wyh.lotterysystem.dao.dataobject.ActivityDo;
import com.wyh.lotterysystem.dao.mapper.ActivityMapper;
import com.wyh.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.wyh.lotterysystem.dao.mapper.ActivityUserMapper;
import com.wyh.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.wyh.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.wyh.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends AbstractActivityOperator{

    @Autowired
    ActivityMapper activityMapper;

    @Autowired
    ActivityUserMapper activityUserMapper;

    @Autowired
    ActivityPrizeMapper activityPrizeMapper;

    @Override
    public int sequence() {
        return 2;
    }

    @Override
    public boolean convertStatus(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getTargetActivityStatus().name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Integer targetId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetStatus = convertActivityStatusDTO.getTargetActivityStatus();

        if (null == targetStatus
                || null == targetId){
            return false;
        }

        //活动存在
        ActivityDo activityDo = activityMapper.selectActivityById(convertActivityStatusDTO.getActivityId());
        if (null == activityDo) {
            return false;
        }

        //活动状态与目标状态不能一样
        if (activityDo.getStatus().equalsIgnoreCase(targetStatus.name())){
            return false;
        }

        //奖品必须全部抽取完成
        int count = activityPrizeMapper.countInitPrize(targetId, ActivityPrizeStatusEnum.INIT.name());
        if (count > 0){
            return false;
        }

        return true;
    }
}
